package com.module.framework.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.framework.domain.HisLend;
import com.module.framework.domain.LoginUser;
import com.module.framework.dto.HisLendDTO;
import com.module.framework.mapper.HisLendMapper;
import com.module.framework.service.HisLendService;
import com.module.framework.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HisLendServiceImpl extends ServiceImpl<HisLendMapper, HisLend> implements HisLendService {

    @Autowired
    private HisLendMapper hisLendMapper;

    @Override
    public PageVO hisLendList(HisLendDTO hisLendDTO) {
        Integer pageNum = hisLendDTO.getPageNum();
        Integer pageSize = hisLendDTO.getPageSize();
        String name = hisLendDTO.getName();
        String author = hisLendDTO.getAuthor();
        Integer cateId = hisLendDTO.getCateId();
        String userName = hisLendDTO.getUserName();
        pageNum = (pageNum - 1) * pageSize;

        PageVO pageVO = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long userId = loginUser.getUser().getId();
        Long roleId = loginUser.getUser().getRoleId();

        if (roleId != 3) {
            List<HisLend> lists = hisLendMapper.selectAllList(pageNum, pageSize, name, cateId, author, userName);
            pageVO = new PageVO(lists, lists.size());
        } else {
            // 当前用户
            List<HisLend> lists = hisLendMapper.selectByIdList(userId, pageNum, pageSize);
            pageVO = new PageVO(lists, lists.size());
        }
        return pageVO;
    }

    @Override
    public HisLend getHisLendInfo(Integer id) {
        HisLend hisLend = hisLendMapper.selectByIdVO(id);
        return hisLend;
    }
}
